﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Compilation;
using System.Web;

namespace Query2Object
{
    public class EntityProvider:BuildProvider
    {
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            base.GenerateCode(assemblyBuilder);
            using (TextWriter tx = assemblyBuilder.CreateCodeFile(this))
            {
                if (tx == null)
                {
                    return;
                }
                StringBuilder code = GetCode();
                tx.Write(code.ToString());
                tx.Flush();
            }           
        }

        private StringBuilder GetCode()
        {
            StringBuilder uss = new StringBuilder();
            List<string> usings = new List<string>();
            usings.Add("using System;");
            usings.Add("using System.Configuration;");
            usings.Add("using System.Collections.Generic;");
            usings.Add("using System.Data;");
            usings.Add("using System.Text;");
            usings.Add("using Query2Object;");
            StringBuilder code = new StringBuilder();
            IDbContext ctx = null;
            IStruts sts = null;
            foreach (string key in Configs.Keys)
            {
                ctx = DllLoader.TryLoadContext(Configs.dbtypes[key]);
                ctx.ConnectionString = Configs.connections[key];
                code.Append(ctx.TableSchema().PrintCode(key));
                sts = ctx.TableSchema();
                foreach (string us in sts.SpecialUsing)
                {
                    if (!usings.Contains(us))
                    {
                        usings.Add(us);
                    }
                }
            }
            foreach (string us in usings)
            {
                uss.Append(us).Append("\r\n");
            }
            code.Append("public class DateSettings\r\n");
            code.Append("{\r\n");
            foreach (string key in Configs.Keys)
            {
                code.Append("\tpublic const string ").Append(key).Append(" = \"").Append(key).Append("\";\r\n");
            }
            code.Append("}\r\n");
            code = uss.Append(code);
            return code;
        }
        public string CreateCode(string FilePath,string NameSpace)
        {
            StringBuilder code = new StringBuilder();
            code.Append("namespace ").Append(NameSpace).Append("\r\n");
            code.Append("{\r\n");
            code.Append(GetCode().ToString());
            code.Append("\r\n}\r\n");
            using (StreamWriter sw = new StreamWriter(FilePath, false, UTF8Encoding.UTF8))
            {
                sw.Write(code.ToString());
                sw.Flush();
                sw.Close();
            }
            return code.ToString();
        }
    }
}
